<template>
  <el-dialog
    v-model="visible"
    width="1000px"
    :close-on-click-modal="false"
    append-to-body
    class="unified-dialog detail-dialog-stats"
  >
    <template #header>
      <DialogHeader
        icon="DataAnalysis"
        title="带统计卡片的详情示例"
        description="使用统计卡片展示关键指标和数据趋势"
        type="primary"
      />
    </template>
    
    <div v-if="data" class="dialog-content">
      <!-- 基本信息 -->
      <div class="detail-section">
        <div class="section-title">
          <el-icon><Document /></el-icon>
          <span>基本信息</span>
        </div>
        <el-descriptions :column="3" border>
          <el-descriptions-item label="ID">{{ data.id }}</el-descriptions-item>
          <el-descriptions-item label="名称">
            <el-tag type="primary" size="small">{{ data.topic_name }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="分区ID">{{ data.partition_id }}</el-descriptions-item>
          <el-descriptions-item label="状态">
            <el-tag :type="getStatusType(data.status)" size="small">
              {{ getStatusText(data.status) }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="监控状态">
            <el-tag :type="data.is_monitored ? 'success' : 'info'" size="small">
              {{ data.is_monitored ? '已启用' : '已禁用' }}
            </el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="告警状态">
            <el-tag :type="getAlertStatusType(data.alert_status)" size="small">
              {{ getAlertStatusText(data.alert_status) }}
            </el-tag>
          </el-descriptions-item>
        </el-descriptions>
      </div>
      
      <!-- 统计信息 -->
      <div class="detail-section">
        <div class="section-title">
          <el-icon><TrendCharts /></el-icon>
          <span>统计信息</span>
          <el-button size="small" @click="refreshStats" :loading="refreshing" style="margin-left: auto;">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
        
        <div class="stats-cards">
          <div class="stat-card">
            <div class="stat-icon offset">
              <el-icon><DataBoard /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ formatNumber(data.latest_offset) }}</div>
              <div class="stat-label">最新Offset</div>
            </div>
          </div>
          
          <div class="stat-card">
            <div class="stat-icon lag">
              <el-icon><Warning /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-number" :class="getLagClass(data.consumer_lag)">
                {{ formatNumber(data.consumer_lag) }}
              </div>
              <div class="stat-label">消费滞后</div>
            </div>
          </div>
          
          <div class="stat-card">
            <div class="stat-icon size">
              <el-icon><Files /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ formatBytes(data.topic_size_bytes) }}</div>
              <div class="stat-label">Topic大小</div>
            </div>
          </div>
          
          <div class="stat-card">
            <div class="stat-icon alert">
              <el-icon><Bell /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-number" :class="data.alert_count > 0 ? 'text-danger' : ''">
                {{ data.alert_count }}
              </div>
              <div class="stat-label">告警次数</div>
            </div>
          </div>
          
          <div class="stat-card">
            <div class="stat-icon threshold">
              <el-icon><Clock /></el-icon>
            </div>
            <div class="stat-content">
              <div class="stat-number">{{ data.alert_threshold_hours }}</div>
              <div class="stat-label">告警阈值(小时)</div>
            </div>
          </div>
        </div>
      </div>
      
      <!-- 详细数据 -->
      <div class="detail-section">
        <div class="section-title">
          <el-icon><List /></el-icon>
          <span>详细数据</span>
        </div>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="最新消息时间">{{ data.message_timestamp || '-' }}</el-descriptions-item>
          <el-descriptions-item label="消息Key">{{ data.message_key || '-' }}</el-descriptions-item>
          <el-descriptions-item label="最大告警次数">{{ data.max_alert_count }}</el-descriptions-item>
          <el-descriptions-item label="首次告警时间">{{ data.first_alert_time || '-' }}</el-descriptions-item>
          <el-descriptions-item label="最后告警时间">{{ data.last_alert_time || '-' }}</el-descriptions-item>
          <el-descriptions-item label="创建时间">{{ data.created_at || '-' }}</el-descriptions-item>
        </el-descriptions>
      </div>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import { Document, TrendCharts, Refresh, DataBoard, Warning, Files, Bell, Clock, List } from '@element-plus/icons-vue'
import DialogHeader from '@/components/DialogHeader.vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => ({})
  }
})

const emit = defineEmits(['update:modelValue'])

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const refreshing = ref(false)

const handleClose = () => {
  visible.value = false
}

const refreshStats = async () => {
  refreshing.value = true
  // 模拟刷新
  await new Promise(resolve => setTimeout(resolve, 1000))
  ElMessage.success('统计信息已刷新（演示模式）')
  refreshing.value = false
}

const formatNumber = (num) => {
  if (!num && num !== 0) return '-'
  return num.toLocaleString()
}

const formatBytes = (bytes) => {
  if (!bytes || bytes === 0) return '0 B'
  const k = 1024
  const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
  const i = Math.floor(Math.log(bytes) / Math.log(k))
  return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i]
}

const getLagClass = (lag) => {
  if (lag > 1000) return 'text-danger'
  if (lag > 100) return 'text-warning'
  return ''
}

const getStatusType = (status) => {
  const typeMap = {
    'active': 'success',
    'inactive': 'info',
    'monitored_only': 'warning'
  }
  return typeMap[status] || 'info'
}

const getStatusText = (status) => {
  const textMap = {
    'active': '活跃',
    'inactive': '非活跃',
    'monitored_only': '仅监控'
  }
  return textMap[status] || status
}

const getAlertStatusType = (status) => {
  const typeMap = {
    'normal': 'success',
    'alerting': 'danger'
  }
  return typeMap[status] || 'info'
}

const getAlertStatusText = (status) => {
  const textMap = {
    'normal': '正常',
    'alerting': '告警中'
  }
  return textMap[status] || status
}
</script>

<style scoped>
.detail-section {
  margin-bottom: 24px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 12px;
  border: 1px solid #e4e7ed;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.detail-section:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.detail-section:last-child {
  margin-bottom: 0;
}

.section-title {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 2px solid #409eff;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
}

.section-title .el-icon {
  margin-right: 8px;
  font-size: 18px;
  color: #409eff;
  background: rgba(64, 158, 255, 0.1);
  padding: 8px;
  border-radius: 8px;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-top: 16px;
}

.stat-card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  background: white;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.stat-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.stat-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 8px;
  font-size: 24px;
  color: white;
}

.stat-icon.offset {
  background: linear-gradient(135deg, #409eff 0%, #66b3ff 100%);
}

.stat-icon.lag {
  background: linear-gradient(135deg, #e6a23c 0%, #f0c78a 100%);
}

.stat-icon.size {
  background: linear-gradient(135deg, #67c23a 0%, #85ce61 100%);
}

.stat-icon.alert {
  background: linear-gradient(135deg, #f56c6c 0%, #f78989 100%);
}

.stat-icon.threshold {
  background: linear-gradient(135deg, #909399 0%, #b1b3b8 100%);
}

.stat-content {
  flex: 1;
}

.stat-number {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  line-height: 1.2;
  margin-bottom: 4px;
}

.stat-number.text-danger {
  color: #f56c6c;
}

.stat-number.text-warning {
  color: #e6a23c;
}

.stat-label {
  font-size: 12px;
  color: #909399;
  font-weight: 500;
}
</style>

